<template>

    

    <div class="page attendance-info">
        <div class="section location-detect com-border-bottom">
            <div class="title-box">
                <h2>当前地址</h2>
            </div>
            <p class="location">{{pageDate.position_name}}（{{pageDate.address}}）</p>
        </div>
        <imageview :is-requird="require_field.photo" :img-array.sync="imgArray" :img-date.sync="imgDate" :allow-album="allow_album"></imageview>
        <div class="section com-border-bottom">
            <div class="title-box com-border-bottom">
                <h2>备注信息<em v-if="require_field.remark == 1">(必填)</em></h2>
            </div>
            <div class="remark-wrap">
                <textarea v-model="remarks" class="remark-input" placeholder="请输入备注信息" maxlength="50"></textarea>
            </div>            
        </div>        
    </div>
</template>

<script>

    let coordinate = ''
    let position_name = ''
    let wait_time = 0//页面等待时间
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import widget from 'gapi/widget'
    import clientUi from 'gapi/clientUi'
    import * as util from 'js/util.js'
    import commonPage from 'clientApi/commonPage.js'
    import cache from 'gapi/cache.js'
    import location from 'gapi/location.js'
    // import clientInfo from 'gapi/clientInfo'

    /**
     *  引入组件 以及 组件接口(Action/Getters)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */
    import Imageview from 'components/Imageview/Imageview'

     
    /**
     *  组件实例对象
     */
    export default {
        components: {
            // Location,
            Imageview
        },
        data: function(){
            return{
                allow_album: false,//是否允许选择相册图片
                // position: '',
                imgArray: [],
                remarks: '',
                imgDate: {
                    pos:'',
                    name: ''
                },
                pageDate: {},//用户信息+定位信息
                sign_type: 0,
                require_field: {}//必填项
            }
        },
        computed:{
            photoDate(){
                let list = []
                this.imgArray.forEach((item)=>{
                    list.push(item.url)
                })
                return list
            },
            water_mark(){
                return this.allow_album ? 1 : 0
            }
        },
        vuex: {
            getters: {
                // getLoc,
                // getImageArray
            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                //页面等待时间
                setInterval(() => {
                    wait_time ++ 
                }, 1000)
                

                this.pageDate = JSON.parse(util.getUrddataString('user_info_sign'))
                // alert(JSON.stringify(this.pageDate))
                this.imgDate.name = this.pageDate.user_name
                this.imgDate.pos = this.pageDate.address
                coordinate = this.pageDate.coordinate
                position_name = this.pageDate.position_name

                // return
                //必填项
                util.ajax({
                    url: '/sign/project/setting',
                    type:'GET',
                    data: {
                        team_id: this.pageDate.team_id,
                        project_id: this.pageDate.project_id
                    },
                    success: (res) => {
                        // alert(JSON.stringify(res))
                        commonPage.loadPageStatus('loadSuccess')
                        if(res&&res.errno == 0){
                            this.require_field = res.data.setting_data.require_field
                            this.allow_album = res.data.setting_data.allow_album == 1 ? true : false
                        }else{
                            widget.toast(res.errmsg)
                        }
                    },
                    error: (xhr, error) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            submit (){
                if(wait_time >= 300){
                    widget.toast('提交失败,请重试')
                    commonPage.goBack(1,{'key':'editsign_timeout'})
                    return
                }

                let ajaxDate = this.getAjaxDate()
                // alert(JSON.stringify(ajaxDate))
                // console.log(ajaxDate)
                // return

                if(this.require_field.photo==1){
                    // 照片必选验证
                    if(!this.imgArray.length){
                        widget.toast('请提交图片')
                        return
                    }
                }
                if(this.require_field.remark==1){
                    // 备注验证
                    if(this.remarks === ''){
                        widget.toast('请填写备注')
                        return
                    }
                }

                // 发送网络请求
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url: '/sign/user/submit',
                    type:'POST',
                    data: ajaxDate,
                    success: (res) => {
                        // alert(JSON.stringify(res))
                        commonPage.loadPageStatus('loadSuccess')
                        if(res&&res.errno==0){
                            commonPage.goBack(1,{'key':'editsign_success'})
                        }else if(res.errmsg){
                             widget.toast(res.errmsg)
                        }
                    },
                    error: (xhr, error) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            getAjaxDate(){
                let date = {
                    team_id: this.pageDate.team_id,
                    project_id: this.pageDate.project_id,
                    img: JSON.stringify(this.photoDate),
                    address: this.imgDate.pos,
                    Lng_Lat: coordinate,
                    position_name: position_name,
                    desc: this.remarks,
                    water_mark: this.water_mark
                }
                
                return date
            }
        },
        created(){
            this.init()
            /**
             * 设置提交按钮
             * @type {String}
             */
            commonPage.updateTitleBar({
                backAction: '',
                leftDisplay: false,
                leftText: '',
                leftAction: '',
                rightDisplay: true,
                rightText: '提交',
                rightAction: 'doSubmit'
            })

            window.doSubmit = this.submit
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/attendance.css"></style>



